
import moment from '../../../utils/moment.js'
var app = getApp()
Page({
  data: {
    days_style:[],
    statementList:[],
    stockList:[],
    partss:[],
    vehicles:[],
    shopList:[],
    shop:[],
    shopChoose:{},
    shopType:0,
    vehicleStock:'',
    partsStock:'',
    timeType:0,
    stockIndex:0,
    allConfirm:false,
    calendarShow:false,
    year: new Date().getFullYear()+"",      // 年份
    month: new Date().getMonth() + 1<10?"0"+(new Date().getMonth()+1):new Date().getMonth() + 1+'',    // 月份
    day: new Date().getDate()+"",
    prev: false,
    next: false,
    pickerView:false,
    current:0,
    startTime:'',
    endTime:'',
    accountId:'',
    stockAgentId:'',
    agentId:''
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      let agentAdminName = res.data.vipUserName
      this.setData({
        agentAdminName
      })
      this.getShopList(res.data.vipUserName)
      this.getTime()
      this.getCashierList('',this.data.startTime,this.data.endTime)
      this.getBrandStock('',this.data.startTime,this.data.endTime)
    })
    this.dayChoose()
  },
  onShow: function () {

  },
  //点击日历事件
  dayClick(e) {
    console.log(e)
    this.setData({
      allConfirm:false,
      year:e.detail.year+'',
      month:e.detail.month<10?'0'+e.detail.month:''+e.detail.month,
      day:e.detail.day+''
    })
    this.dayChoose()
  },
  //日期选择
  dayChoose(){
    let days_style=[]
    days_style.push({
      month:'current',
      day:this.data.day,
      color:'#fff',
      background:'#539FFF'
    })
    this.setData({
      days_style,
    })
  },
  //打开日历
  bindTimeCheck(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      calendarShow:true,
      timeType:type
    })
  },
  //日历取消按钮事件
  bindCancel(e){
    this.setData({
      calendarShow:false
    })
  },
  //日历确定按钮事件
  bindConfirm(){
    this.setData({
      calendarShow:false
    })
    this.getTime()
    if (this.data.timeType==0) {
      this.getCashierList(this.data.accountId,this.data.startTime,this.data.endTime)
    }else{
      this.getBrandStock(this.data.agentId,this.data.startTime,this.data.endTime)
    }
  },
  //日历全部按钮事件
  allConfirm(e){
    let days_style=[]
    this.setData({
      allConfirm:true,
      days_style,
    })
  },
  next(e){
    console.log(e.detail.currentMonth)
    this.setData({
      year:e.detail.currentYear+'',
      month:e.detail.currentMonth<10?'0'+e.detail.currentMonth:''+e.detail.currentMonth
    })
  },
  prev(e){
    this.setData({
      year:e.detail.currentYear+'',
      month:e.detail.currentMonth<10?'0'+e.detail.currentMonth:''+e.detail.currentMonth
    })
  },
  getTime(){
    let startTime=''
    let endTime=''
    if (this.data.allConfirm) {
      startTime =  this.data.year+'-'+this.data.month+'-01'
      endTime = moment(startTime).endOf('month').format('YYYY-MM-DD')
      this.setData({
        startTime,
        endTime
      })
    }else{
      startTime = this.data.year+'-'+this.data.month+'-'+this.data.day
      endTime = startTime
      this.setData({
        startTime,
        endTime
      })
    }
  },
  //财务报表
  getCashierList(accountId,startTime,endTime){
    var that = this
    let data = {
          accountId,
          startTime,
          endTime,
          brandId:'',
          agentAdminName:accountId==''?this.data.agentAdminName:''
        }
    console.log(data)
    app.request('wx_app/agent/cashier',data,'GET').then(res=>{
      console.log(res)
      that.setData({
        statementList:res.data
      })
    })
  },
  //获取门店列表
  getShopList(agentAdminName){
    let data = {
      agentAdminName
    }
    app.request('wx_app/agent_account/findByAgentAdmin',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let shopList = []
        let all = {
          username:'',
          company_name:'全部',
          id:'',
          accountId:''
        }
        shopList.push(all)
        res.data.forEach(item=>{
          shopList.push(item)
        })
        this.setData({
          shopList,
          shop:all,
          ['shopChoose.finance']:all,
          ['shopChoose.sale']:all,
          ['shopChoose.stock']:all
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  bindPickerChange(e) {
    console.log(e.detail.value)
    let i = e.detail.value
    this.setData({
      shop:this.data.shopList[i]
    })
  },
  closePicker(){
    this.setData({
      pickerView:false
    })
  },
  openPicker(e){
    this.setData({
      pickerView:true,
      shopType:e.currentTarget.dataset.type
    })
  },
  confirmPicker(){
    if (this.data.shopType=='0') { //财务报表
      this.setData({
        ['shopChoose.finance']:this.data.shop,
        accountId:this.data.shop.accountId
      })
      this.getCashierList(this.data.shop.accountId,this.data.startTime,this.data.endTime)
    }else if (this.data.shopType=='1') { //卖货统计
      this.setData({
        ['shopChoose.sale']:this.data.shop,
        agentId:this.data.shop.id
      })
      this.getBrandStock(this.data.shop.id,this.data.startTime,this.data.endTime)
    }else{ //库存统计
      this.setData({
        ['shopChoose.stock']:this.data.shop,
        stockAgentId:this.data.shop.id
      })
      this.getStock(this.data.shop.id)
    }
    this.setData({
      pickerView:false
    })
  },
  //获取卖货统计
  getBrandStock(agentId,startTime,endTime){
    let data = {
      agentId,
      brandId:'',
      startTime,
      endTime,
      agentAdminName:agentId==''?this.data.agentAdminName:''
    }
    console.log(data)
    app.request('wx_app/user_order/salesStatistics',data,'GET').then(res=>{
      console.log(res)
      let [vehicles,partss] = [[],[]]
      res.data.vehicles.forEach(item=>{
        vehicles.push(item)
      })
      res.data.partss.forEach(item=>{
        partss.push(item)
      })
      this.setData({
        vehicleStock:res.data.vehicle.amount,
        partsStock:res.data.parts.amount,
        oldStock:res.data.oldSku.amount,
        vehicles,
        partss
      })
    })
  },
  //获取库存统计
  getStock(agentId){
    let data = {
      agentId,
      brandId:'',
      agentAdminName:agentId==''?this.data.agentAdminName:''
    }
    console.log(data)
    app.request('wx_app/agent/stock/inventoryData',data,'GET').then(res=>{
      console.log(res)
      let vehicles = []
      let partss = []
      res.data.vehicles.forEach(item=>{
        vehicles.push(item)
      })
      res.data.partss.forEach(item=>{
        partss.push(item)
      })
      this.setData({
        vehicleStock:res.data.vehicle.stock,
        partsStock:res.data.parts.stock,
        oldStock:res.data.oldSku.stock,
        vehicles,
        partss
      })
    })
  },
  getParts(e){
    let i = e.currentTarget.dataset.i
    this.setData({
      stockIndex:i
    })
  },
  getVehicles(e){
    let i = e.currentTarget.dataset.i
    this.setData({
      stockIndex:i
    })
  },
  bindChange(e){
    let current = e.currentTarget.dataset.current
    this.setData({
      current
    })
    if (current==0) {
      this.getBrandStock(this.data.agentId,this.data.startTime,this.data.endTime)
    }else{
      this.getStock(this.data.stockAgentId)
    }
  }
})